package com.yiwei.ydd.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.adapter.VipOrderAdapter;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.PageBean;
import com.yiwei.ydd.api.bean.VipOrderDetailBean;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.event.BuyVipUpSuccessEvent;
import com.yiwei.ydd.util.Dialog;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.Util;
import com.yiwei.ydd.view.OnLoadeMoreListener;
import com.yiwei.ydd.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class VipOrderActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;

    private Unbinder unbiner;
    private VipOrderAdapter adapter;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getVipOrderList(page);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip_order);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
        EventBus.getDefault().register(this);
    }

    private void init() {
        txtTitle.setText("会员订单");
        adapter = new VipOrderAdapter(this);
        adapter.setOnChooseListener(new VipOrderAdapter.OnChooseListener() {
            @Override
            public void onDetail(String order_no) {
                Intent intent = new Intent(VipOrderActivity.this, VipOrderDetailActivity.class);
                intent.putExtra("order_no", order_no);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
            }

            @Override
            public void onRecharge() {
                Util.startActivity(VipOrderActivity.this, PacketRechargeActivity.class);
            }

            @Override
            public void onDelete(String order_no) {
                Dialog.showDefaultDialog(VipOrderActivity.this, "", "确定要删除吗?", "取消", "确定", false, new Dialog.DialogDefaultClickListener() {
                    @Override
                    public void confirm() {
                        getOrderDelete(order_no);
                    }

                    @Override
                    public void cancel() {

                    }
                });
            }

            @Override
            public void onPay(String pay_money, String order_no) {
                Intent intent = new Intent(VipOrderActivity.this, VipOrderPayMoneyActivity.class);
                intent.putExtra("pay_money", pay_money);
                intent.putExtra("order_no", order_no);
                intent.putExtra("is_vip_order", "1");
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
            }
        });
        list.addOnScrollListener(onLoadeMoreListener);
        list.setAdapter(adapter);

        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage();
                getVipOrderList(1);
            }
        });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(BuyVipUpSuccessEvent event) {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getVipOrderList(1);
    }

    public void getOrderDelete(String order_number) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }


        refresh.setRefreshing(true);
        VipOrderDetailBean bean = new VipOrderDetailBean();
        bean.order_number = order_number;
        Api.api_service.getVipOrderDelete(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "删除成功");
                    onLoadeMoreListener.setPage();
                    getVipOrderList(1);
                });
    }

    public void getVipOrderList(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }


        refresh.setRefreshing(true);
        PageBean bean = new PageBean();
        bean.page = page;
        Api.api_service.getVipOrderList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        unbiner.unbind();
        unbiner = null;
    }


    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }
}
